package com.czk.classreader;

import com.czk.utils.FileUtil;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

import java.io.IOException;

/**
 * 增加cloneable接口
 *
 * @Author:ChenZhangKun
 * @Date: 2021/8/4 17:52
 */
public class ChangeInterfaceCore {
    public static void main(String[] args) throws IOException {
        String path = "F:\\IdeaProjects\\asm\\target\\classes\\com\\czk\\sample\\Test.class";
        byte[] bytes1 = FileUtil.readBytes(path);
        ClassReader cr = new ClassReader(bytes1);
        // cw
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        // 串联
        int api = Opcodes.ASM9;
        ClassVisitor cv = new ClassCloneVisitor(api, cw);
        //（4）结合ClassReader和ClassVisitor
        int parsingOptions = ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
        cr.accept(cv, parsingOptions);
        byte[] bytes = cw.toByteArray();
        FileUtil.writeBytes(path, bytes);
    }
}
